<template>
    <section>
        <!--工具条-->
        <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
            <el-form :model="filters" :inline="true">
                <el-form-item>
                    <el-input v-model="filters.keywords" placeholder="关键字"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" v-on:click="getTenantTypes">查询</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="handleAdd">新增</el-button>
                </el-form-item>
            </el-form>
        </el-col>

        <!--列表v-loading="listLoading"-->
        <el-table :data="tenantTypes" v-loading="listLoading" highlight-current-row @selection-change="selsChange" style="width: 100%;">
            <!--多选框-->
            <el-table-column type="selection" width="55">
            </el-table-column>
            <!--索引值,为什么不用id,id不序号-->
            <el-table-column type="index" width="60">
            </el-table-column>
            <!--其他都设置值,只有一个不设置值就自动适应了-->
            <el-table-column prop="name" label="名称">
            </el-table-column>
            <el-table-column prop="description" label="描述">
            </el-table-column>
            <el-table-column label="操作" width="150">
                <template scope="scope">
                    <el-button size="small" @click="handleEdit(scope.row)">编辑</el-button>
                    <el-button type="danger" size="small" @click="del(scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <!--工具条-->
        <el-col :span="24" class="toolbar">
            <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
            <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="10"
                           :total="total" style="float:right;">
            </el-pagination>
        </el-col>
        <!--模态款-->
        <!--新增界面-->
        <el-dialog title="新增" :visible.sync="addFormVisible"  :close-on-click-modal="false">
            <el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">
                <el-form-item label="部门名称" prop="name">
                    <el-input v-model="addForm.name" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="部门描述" prop="description">
                    <el-input v-model="addForm.description" auto-complete="off"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="addFormVisible = false">取消</el-button>
                <el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
            </div>
        </el-dialog>



    </section>
</template>

<script>
    export default {
        data() {
            return {
                sels:[],
                addFormVisible: false,
                listLoading: false,
                //查询对象
                filters: {
                    keywords: ''
                },
                page: 1,//当前页,要传递到后台的
                total: 0, //分页总数
                tenantTypes: [], //当前页数据
                //新增界面数据
                addForm: {
                    name: '',
                    description: '',
                },
                addFormRules: {
                    name: [
                        { required: true, message: '请输入姓名', trigger: 'blur' }
                    ]
                },
                addLoading:false,
            }
        },
        methods: {
            //复选勾选框的方法
            selsChange: function (sels) {
                this.sels = sels;
            },
            //批量删除
            batchRemove: function () {
                var ids = this.sels.map(item => item.id)//去掉tostring
                this.$confirm('确认删除选中记录吗？', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;
                    //NProgress.start();
                    let para = { ids: ids };
                    console.log(ids)
                    this.$http.post("/system/tenantType/batchRemove",para).then((res) => {
                        this.listLoading = false;
                        //NProgress.done();
                        this.$message({
                            message: '删除成功',
                            type: 'success'
                        });
                        this.getTenantTypes();
                    });
                }).catch(() => {

                });
            },
            //显示编辑界面
            handleEdit: function (row) {
                //显示编辑页面的模态框
                this.addFormVisible = true;
                this.addForm = row;
            },
            //显示新增界面
            handleAdd: function () {
                this.addFormVisible = true;
                this.addForm = {
                    name: '',
                    description: '',
                };
            },
            //新增
            addSubmit: function () {
                //$refs调用js 原生dom元素
                //validate
                this.$refs.addForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            //这里才是我们处理数据的地方
                            this.addLoading = true;
                            //Object.assign 复制对象的
                            let para = Object.assign({}, this.addForm);
                            this.$http.post("/system/tenantType/save",para).then((res) => {
                                console.log(res)
                                this.addLoading = false;
                                //elementUi的提示弹框
                                this.$message({
                                    message: res.data.success?'操作成功':res.data.message,
                                    type: res.data.success?'success':'error'
                                });
                                //重新set数据
                                this.$refs['addForm'].resetFields();
                                this.addFormVisible = false;
                                //刷新数据
                                this.getTenantTypes();
                            });
                        });
                    }
                });
            },
			del(row){
				this.$http.delete("/system/tenantType/"+row.id,null).then(result => {
					if(result.data.success){
						this.$message({
							message: result.data.message,
							type: 'success'
						});
					}else {
						this.$message.error(result.data.message);
					}
					this.getTenantTypes();
				});
			},
            handleCurrentChange(curentPage) {
                this.page = curentPage;
                this.getTenantTypes();
            },
            getTenantTypes() {
                //发送Ajax请求后台获取数据  axios
                //添加分页条件及高级查询条件
                let para = {
                    "page": this.page,
                    "keyword": this.filters.keywords
                };
                this.listLoading = true; //显示加载圈
                //分页查询
                this.$http.post("/system/tenantType/pagelist", para).then(result => {
                    let {data, success, message} = result.data;
                    this.total = data.total;
                    this.tenantTypes = data.rows;
                    this.listLoading = false;  //关闭加载圈
                });
                /* 查询所有
                this.$http.patch("/department",para) //$.Post(.....)
                    .then(result=>{
                        console.log(result);
                        console.log(result.data);
                        this.tenantTypes = result.data;
                        this.listLoading = false;  //关闭加载圈
                    });
                    */
            }
        },
        mounted() {
            this.getTenantTypes()
        }
    }

</script>

<style scoped>

</style>